//
//  CYMinePickerView.m
//  cyydt
//
//  Created by chu on 2019/2/4.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYMinePickerView.h"

@interface CYMinePickerView ()
{
    NSArray *_dataArr;
    NSInteger _components;
    NSInteger selectRowOneComponent;
    NSInteger selectRowTwoComponent;
}

@end

@implementation CYMinePickerView

- (instancetype)initWithFrame:(CGRect)frame DataArr:(NSArray *)dataArr components:(NSInteger)components{
    if (self = [super initWithFrame:frame]) {
        _dataArr = dataArr;
        _components = components;
        self.backgroundColor = [UIColor whiteColor];
        [self initUI];
    }
    return self;
}

- (void)initUI{
    UIView *upView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWindowW, 50)];
    self.titleLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];
    self.titleLable.font = [UIFont systemFontOfSize:18];
    self.titleLable.textColor = RGBOF(0x333333);
    self.titleLable.textAlignment = NSTextAlignmentCenter;
    self.titleLable.center = upView.center;
    [upView addSubview:self.titleLable];
    
    UIButton *closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(kWindowW - 50, 0, 50, 50)];
    [closeBtn setImage:[UIImage imageNamed:@"grzl_gb"] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(closeAction:) forControlEvents:UIControlEventTouchUpInside];
    [upView addSubview:closeBtn];

    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 49, kWindowW, 1)];
    lineView.backgroundColor = RGBOF(0xe3e3e3);
    [upView addSubview:lineView];

    [self addSubview:upView];
    
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 75, kWindowW, 75)];
    UIButton *doneBtn = [[UIButton alloc] initWithFrame:CGRectMake(20, 15, self.frame.size.width - 40, 45)];
    doneBtn.layer.cornerRadius = 45 / 2;
    doneBtn.layer.masksToBounds = YES;
    [doneBtn setTitle:@"确定" forState:UIControlStateNormal];
    [doneBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    doneBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [doneBtn setBackgroundColor:RGBOF(0xFF5353)];
    [doneBtn addTarget:self action:@selector(doneAction:) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:doneBtn];
    
    UIView *lineView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWindowW, 1)];
    lineView1.backgroundColor = RGBOF(0xe3e3e3);
    [bottomView addSubview:lineView1];
    [self addSubview:bottomView];
    
    UIPickerView *pickerView = [[UIPickerView alloc] init];
    pickerView.delegate = self;
    pickerView.dataSource = self;
    [self addSubview:pickerView];
    
    [pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(upView.mas_bottom).with.mas_equalTo(0);
        make.left.equalTo(self).with.mas_equalTo(0);
        make.bottom.equalTo(bottomView.mas_top).with.mas_equalTo(0);
        make.right.equalTo(self).with.mas_equalTo(0);
    }];

}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    NSLog(@"_components -- %ld",_components);
    return _components;
}

//返回指定列的行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (_components == 2) {
        CYAddressModel *model = _dataArr[selectRowOneComponent];
        NSArray *citis = model.cities;
        if (component==0) {
            return _dataArr.count;
        }else{
            NSLog(@"citis.count -- %ld",citis.count);
            return citis.count;
        }
    }
    
    return _dataArr.count;
    
}

//返回指定列，行的高度，就是自定义行的高度

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 50;
}

//返回指定列的宽度

- (CGFloat) pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    if (_components == 2) {
        return  self.frame.size.width/2;
    }else{
        return  self.frame.size.width;
    }
}
// 自定义指定列的每行的视图，即指定列的每行的视图行为一致
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    //设置分割线的颜色
    for(UIView *singleLine in pickerView.subviews)
    {
        if (singleLine.frame.size.height < 2)
        {
            singleLine.backgroundColor = RGBOF(0xEDEDED);
        }
    }
    UILabel *reasonLabel = [UILabel new];
    
    if (_components == 2) {
        if (component == 0) {
            CYAddressModel *model = _dataArr[row];
            
            //设置文字的属性（改变picker中字体的颜色大小）
            reasonLabel.textAlignment = NSTextAlignmentCenter;
            reasonLabel.text = model.name;
            reasonLabel.font = [UIFont systemFontOfSize:17];
            reasonLabel.textColor = RGBOF(0x999999);
            //改变选中行颜色（设置一个全局变量，在选择结束后获取到当前显示行，记录下来，刷新picker）
            if (row == selectRowOneComponent) {
                //改变当前显示行的字体颜色，如果你愿意，也可以改变字体大小，状态
                reasonLabel.textColor = RGBOF(0xF43636);
            }
        }else{
            CYAddressModel *model = _dataArr[selectRowOneComponent];
            NSArray *citis = model.cities;
            CYCityModel *citys = citis[row];
            
            //设置文字的属性（改变picker中字体的颜色大小）
            reasonLabel.textAlignment = NSTextAlignmentCenter;
            reasonLabel.text = citys.name;
            reasonLabel.font = [UIFont systemFontOfSize:17];
            reasonLabel.textColor = RGBOF(0x999999);
            //改变选中行颜色（设置一个全局变量，在选择结束后获取到当前显示行，记录下来，刷新picker）
            if (row == selectRowTwoComponent) {
                //改变当前显示行的字体颜色，如果你愿意，也可以改变字体大小，状态
                reasonLabel.textColor = RGBOF(0xF43636);
            }
        }
        
    }else{
        //设置文字的属性（改变picker中字体的颜色大小）
        reasonLabel.textAlignment = NSTextAlignmentCenter;
        reasonLabel.text = _dataArr[row];
        reasonLabel.font = [UIFont systemFontOfSize:17];
        reasonLabel.textColor = RGBOF(0x999999);
        //改变选中行颜色（设置一个全局变量，在选择结束后获取到当前显示行，记录下来，刷新picker）
        if (row == selectRowOneComponent) {
            //改变当前显示行的字体颜色，如果你愿意，也可以改变字体大小，状态
            reasonLabel.textColor = RGBOF(0xF43636);
        }
    }
    
    
    return reasonLabel;
}

//显示的标题

//- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
//    if (_components == 2) {
//        if (component == 0) {
//            CYAddressModel *model = _dataArr[component];
//            return model.name;
//        }else{
//            CYAddressModel *model = _dataArr[selectRow];
//            NSArray *citis = model.cities;
//            CYCityModel *citys = citis[row];
//            return citys.name;
//        }
//    }else{
//        return _dataArr[component];
//    }
//}

//显示的标题字体、颜色等属性

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component{
    NSString *str = @"";
    if (_components == 1) {
        str = [_dataArr objectAtIndex:row];
    }else{
        
        
        if (component == 0) {
            CYAddressModel *model = _dataArr[row];
            str = model.name;
        }else{
            CYAddressModel *model = _dataArr[selectRowOneComponent];
            
            NSArray *citis = model.cities;
            CYCityModel *citys = citis[row];
            str = citys.name;
        }
    }
    
    NSMutableAttributedString *AttributedString = [[NSMutableAttributedString alloc]initWithString:str];
    
    [AttributedString addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18], NSForegroundColorAttributeName:[UIColor blackColor]} range:NSMakeRange(0, [AttributedString  length])];
    
    return AttributedString;
    
}//NS_AVAILABLE_IOS(6_0);

//被选择的行

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component == 0) {
        selectRowOneComponent = row;
    }else{
        selectRowTwoComponent = row;
    }
    [pickerView reloadAllComponents];
}

- (void)closeAction:(UIButton *)sender{
    [GKCover hide];
}
- (void)doneAction:(UIButton *)sender{
    if (_components == 2) {
        if (self.addressBlock) {
            CYAddressModel *address = _dataArr[selectRowOneComponent];
            NSArray *citis = address.cities;
            CYCityModel *city = citis[selectRowTwoComponent];
            self.addressBlock(address, city);
        }
    }else{
        if (self.otherBlock) {
            NSString *value = _dataArr[selectRowOneComponent];
            self.otherBlock(value, selectRowOneComponent);
        }
    }
    [GKCover hide];
}
@end
